<script>
    $(document).ready(function() {
        function ajaxForm(formId) {
            edsUI.form(formId, function (result) {
                if(result.status) {
                    window.location = '{{ path('admin_' ~ entity ~ '_index') }}';
                } else {
                    edsUI.mask().alert(result.message);
                }

                return false;
            }, ['#saveModalFormBtn','text']);
        }

        ajaxForm('#modalForm');

        $(document).on('click', '[data-edit]', function (e) {
            e.preventDefault();

            var formId =  Math.round(Math.random()*100000000000);

            let url = edsUI.location.init($(this).attr('href'))
                .setParameter('r', formId)
                .build();

            $.get(url, function (res) {
                if (res.status === 0) {
                    showAlertModal(formId, res.message);
                }

                $('body').append(res);

                $('#entityModal'+formId).last().modal();

                ajaxForm('#modalForm'+formId);
            });
        });
    });
</script>
